import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import './assets/css/bootstrap.min.css'
import './assets/css/bootstrap.min.js'
import Antd from 'ant-design-vue'
import 'jquery/dist/jquery.min'
//import 'bootstrap/dist/css/bootstrap.css'

import axios from 'axios';
import VueAxios from "vue-axios";

// const request = axios.create({
//     baseURL: '/api',
//     timeout: 5000
// })
const whiteUrls = ['admin/login','entity/not_pass','entity/new']
// request 拦截器
axios.interceptors.request.use(config => {
    config.headers['Content-Type'] = 'application/json;charset=utf-8';

    let userJson = sessionStorage.getItem("user")
    let adminJson = sessionStorage.getItem("admin")
    if (!whiteUrls.includes(config.url)) {
        if (!userJson && !adminJson) {
            router.push("/login");
        } else {
            let user = JSON.parse(userJson);
            config.headers['token'] = user.token;  // 设置请求头
        }
    }

    return config
}, error => {
    return Promise.reject(error)
});

// response 拦截器
// 可以在接口响应后统一处理结果
axios.interceptors.response.use(
    response => {
        let res = response.data;
        // 如果是返回的文件
        if (response.config.responseType === 'blob') {
            return res
        }
        // 兼容服务端返回的字符串数据
        if (typeof res === 'string') {
            res = res ? JSON.parse(res) : res
        }
        return res;
    },
    error => {
        console.log('err' + error) // for debug
        return Promise.reject(error)
    }
)

axios.defaults.baseURL = 'http://127.0.0.1:9090'
axios.defaults.timeout = 5000
const app = createApp(App).use(store).use(router).use(ElementPlus).use(VueAxios, axios)
for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
}
app.mount('#app')